package com.atguigu.system.controller;
import com.atguigu.model.system.SysRole;
import com.atguigu.model.vo.AssginRoleVo;
import com.atguigu.model.vo.SysRoleQueryVo;
import com.atguigu.system.annotation.Log;
import com.atguigu.system.common.result.Result;
import com.atguigu.system.enums.BusinessType;
import com.atguigu.system.exception.CustomerException;
import com.atguigu.system.service.SysRoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Log(title = "角色管理", businessType = BusinessType.SELECT)
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "获取分页列表")
    @GetMapping("/{page}/{limit}")
    public Result selectByPage(
        @ApiParam(name = "page", value = "当前页码", required = true)
        @PathVariable Long page,

        @ApiParam(name = "limit", value = "每页记录数", required = true)
        @PathVariable Long limit,

        SysRoleQueryVo roleQueryVo){
        Page<SysRole> pageParam = new Page<>(page, limit);

        //执行分页方法
        IPage<SysRole> pageModel = sysRoleService.selectPage(pageParam, roleQueryVo);

        //返回分页对象
        return Result.ok(pageModel);
    }

    @Log(title = "角色管理", businessType = BusinessType.SELECT)
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation(value = "获取角色")
    @GetMapping("/get/{id}")
    public Result getById(
            @ApiParam(name = "id", value = "角色id", required = true)
            @PathVariable Long id) {
        SysRole role = sysRoleService.getById(id);
        return Result.ok(role);
    }


    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation(value = "新增角色")
    @PostMapping("/save")
    public Result save(
            @ApiParam(name = "sysRole", value = "角色对象", required = true)
            @RequestBody SysRole sysRole) {
        sysRoleService.save(sysRole);
        return Result.ok();
    }

    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation(value = "修改角色")
    @PutMapping("/update")
    public Result updateById(
            @ApiParam(name = "sysRole", value = "角色对象", required = true)
            @RequestBody SysRole role) {
        sysRoleService.updateById(role);
        return Result.ok();
    }

    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "删除角色")
    @DeleteMapping("/remove/{id}")
    public Result remove(
            @ApiParam(name = "id", value = "角色id", required = true)
            @PathVariable Long id) {
        sysRoleService.removeById(id);
        return Result.ok();
    }

    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(
            @ApiParam(name = "id", value = "角色id集合", required = true)
            @RequestBody List<Long> idList) {
        sysRoleService.removeByIds(idList);
        return Result.ok();
    }

    @Log(title = "角色管理", businessType = BusinessType.ASSGINRole)
    @PreAuthorize("hasAuthority('bnt.sysUser.assignRole')")
    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable String userId){
       Map<String,Object> roleMap = sysRoleService.getRolesByUserId(userId);
       return Result.ok(roleMap);
    }

    @Log(title = "角色管理", businessType = BusinessType.ASSGINRole)
    @PreAuthorize("hasAuthority('bnt.sysUser.assignRole')")
    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }
}
